package org.itboys.core.service.mysql.web;

import org.apache.commons.lang3.StringUtils;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.dao.coupon.SundayCouponMapper;
import org.itboys.core.dao.coupon.SundayCouponRecordMapper;
import org.itboys.core.entity.mysql.coupon.SundayCoupon;
import org.itboys.core.entity.mysql.coupon.SundayCouponExample;
import org.itboys.core.entity.mysql.coupon.record.SundayMemberCoupon;
import org.itboys.core.entity.mysql.coupon.record.SundayMemberCouponExample;
import org.itboys.core.util.MybatisParamUtil;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author：贾琦磊
 * @Date：2018/12/10
 * @Description：优惠券
 */
@Service
public class SundayCouponService {
    @Autowired
    private SundayCouponMapper couponMapper;
    @Autowired
    private SundayUploadService uploadService;
    @Autowired
    private SundayCouponRecordMapper couponRecordMapper;

    public PageResult<SundayCoupon> select(MybatisParamUtil paramUtil){
        SundayCouponExample example=new SundayCouponExample();
        SundayCouponExample.Criteria criteria = example.createCriteria()
                .andIsDeletedEqualTo(SundayCommonConstants.NO);
        long total = couponMapper.countByExample(example);
        example.setOrderByClause(paramUtil.getSortName());
        example.setSortOrder(paramUtil.getSortOrder());
        example.setPageSize(paramUtil.getRowSize());
        example.setStartRow(paramUtil.getRowStart());
        List<SundayCoupon> data = couponMapper.selectByExample(example);
        return new PageResult<>(data,total);
    }
    public void save(SundayCoupon coupon, MultipartFile image){
        try {
            Map<String, String> backMap = uploadService.uploadSingle(image);
            if(StringUtils.isNotEmpty(backMap.get(SundayUploadService.UPLOAD_HALF_PATH))){
                coupon.setImage(backMap.get(SundayUploadService.UPLOAD_HALF_PATH));
            }
            if(LongUtil.isNotZreo(coupon.getId())){
                couponMapper.update(coupon);
            }else {
                couponMapper.insert(coupon);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public SundayCoupon findOne(Long id){
        return couponMapper.select(id);
    }


}
